import { ref, reactive, onMounted, watch } from 'vue'
import { EditType } from '@/type/BaseEnum'
import useInstance from '@/hooks/useInstance'
import { useRouter, useRoute } from 'vue-router'
import { getZiTiListApi } from '@/api/manage'
import { ZiTiParm } from '@/api/manage/MangeModel'
export default function useAddress() {
	//新增弹框的ref属性
	const addRef = ref<{ show: (type?: string, row?: any) => void }>()
	const router = useRouter()
	const route = useRoute()
	const flagmark = ref<any>(0);
	const hasMore = ref(true)
	//表格数据
	const listData = reactive({
		list: [] as any
	})
	//列表查询的参数
	const listParm = reactive<ZiTiParm>({
		pagesize: 10,
		page: 1,
		count: 0
	})
	//获取全局属性
	const { global } = useInstance()
	//新增
	const addBtn = () => {
		addRef.value?.show(EditType.ADD)
	}
	//编辑
	const editBtn = (row: any) => {
		addRef.value?.show(EditType.EDIT, row)
	}

	const getList = async (parm: ZiTiParm) => {
		let res = await getZiTiListApi(parm)
		if (res && res.code == 0) {
			const { list, count } = res.result
			if (list && list.length <= 0) return (hasMore.value = false)
			listData.list = listData.list.concat(list)
			listParm.count = count
		}
	}
	const reFresh = () => {
		listData.list = []
		listParm.page = 1
		getList(listParm)
		if(flagmark.value==1){
			router.go(-1)
		}
	}
	const loadMore = async (done: any) => {
		listParm.page = (listParm.page as number) * 1 + 1
		await getList(listParm)
		done()
	}
	 

	onMounted(() => {
		const {flag} = route.query;
			if (flag == '1') {
				flagmark.value = 1
				addBtn()
			}
		getList(listParm)
		 
	})
	return {
		getList,
		addRef,
		addBtn,
		editBtn,
		reFresh,
		listData,
		listParm,
		hasMore,
		loadMore,
		flagmark
	}
}
